package javasec.contempt.dialogs;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Date;

import javasec.contempt.Activator;
import javasec.contempt.consoles.SeedLogConsole;
import javasec.contempt.data.SeedConnector;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;

import sun.security.x509.CertAndKeyGen;
import sun.security.x509.X500Name;

public class StartSeedDialog extends Dialog
{	
	public String filename=null;
	public boolean runAsRoot=false;
	public boolean runLocalhost=true;
	
	public Text filenameText=null;
	public Button filenameButton=null;
	public Button rootCheckbox=null;
	public Button localhostCheckbox=null;
	public boolean aborted=true;
	
	public StartSeedDialog(Shell parent)
	{
		super(parent);
	}
		
	protected void okPressed()
	{
		filename=filenameText.getText();
		runAsRoot=rootCheckbox.getSelection();
		runLocalhost=localhostCheckbox.getSelection();
		
		File seedServerFile=new File(SeedConnector.contemptPath+File.separator+"SeedServer.jar");
		
		if(!seedServerFile.exists())
		{
			seedServerFile=new File(filename);
		}
		
		if(!seedServerFile.exists())
		{
			System.out.println(seedServerFile.getPath());
			System.out.println("error: could not find SeedServer.jar");
			return;
		}

		Process seedProcess=null;
		try
		{
			String[] cmd=null;
			if(!runAsRoot&&runLocalhost)cmd=new String[] {"java","-jar",seedServerFile.getPath(),"-p",SeedConnector.globalPass,"-b","127.0.0.1"};
			if(!runAsRoot&&!runLocalhost)cmd=new String[] {"java","-jar",seedServerFile.getPath(),"-p",SeedConnector.globalPass};
			if(runAsRoot&&runLocalhost)cmd=new String[] {"gksu","java -jar "+seedServerFile.getPath()+" -p "+SeedConnector.globalPass+" -b 127.0.0.1 -h "+System.getProperty("user.home")+File.separator+".seed"};
			if(runAsRoot&&!runLocalhost)cmd=new String[] {"gksu","java -jar "+seedServerFile.getPath()+" -p "+SeedConnector.globalPass+" -h "+System.getProperty("user.home")+File.separator+".seed"};
			
			seedProcess=Runtime.getRuntime().exec(cmd);
			Thread.sleep(2600);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		// open the log console
		SeedLogConsole inputConsole=new SeedLogConsole("seed log console",null);
		inputConsole.setStream(seedProcess.getInputStream());
	
		SeedLogConsole errorConsole=new SeedLogConsole("seed error log console",null);
		errorConsole.setStream(seedProcess.getErrorStream());
	
		ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{errorConsole});
		ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{inputConsole});
	
		aborted=false;
		super.okPressed();
	}
	
	protected Control createDialogArea(Composite parent)
	{
		Composite composite1 = new Composite(parent,0); // encompassing composite
		composite1.setLayout(new GridLayout(1,false));
		
		Composite composite2 = new Composite(composite1,GridData.VERTICAL_ALIGN_BEGINNING); // top row
		composite2.setLayout(new GridLayout(3,false));
		
		Composite composite3 = new Composite(composite1,0); // bottom row
		composite3.setLayout(new GridLayout(2,false));	
	
		Label filenameLabel=new Label(composite2,SWT.None);
		filenameLabel.setText("jarfile");
		filenameText=new Text(composite2,SWT.LEFT|SWT.BORDER);
		filenameText.setText(System.getProperty("user.home")+
			File.separator+".contempt"+File.separator+"SeedServer.jar");
		filenameButton=new Button(composite2,SWT.PUSH);
		filenameButton.setText("browse");
		filenameText.setLayoutData(new GridData(GridData.FILL_VERTICAL));

		rootCheckbox=new Button(composite3,SWT.CHECK);
		rootCheckbox.setText("run as root");
		//rootCheckbox.setEnabled(false);

		localhostCheckbox=new Button(composite3,SWT.CHECK);
		localhostCheckbox.setText("localhost only");
		localhostCheckbox.setSelection(true); // TODO this should be true by default
		
		// set up what the filename button does
		filenameButton.addSelectionListener(new SelectionListener()
		{
			public void widgetDefaultSelected(SelectionEvent e)
			{
				// I don't think this event ever happens with SWT.PUSH	
			}

			public void widgetSelected(SelectionEvent e)
			{
				// open a file dialog so user can select the jarfile
				FileDialog fileDialog=new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN);
				fileDialog.setFilterPath(System.getProperty("user.home")+File.separator+".contempt");
				fileDialog.setFilterExtensions(new String[]{"*.jar","*"});
				filenameText.setText(fileDialog.open());
			}
		});

		return composite1;
	}
	
	protected void configureShell(Shell newShell)
	{
		super.configureShell(newShell);
		newShell.setText("launch local seedserver");
	}
}
